package cn.wikiflyer.beikang.act;

import java.lang.reflect.Type;

import android.content.Context;
import android.content.Intent;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ImageView;
import android.widget.TextView;
import cn.wikiflyer.beikang.BaseActivity;
import cn.wikiflyer.beikang.R;
import cn.wikiflyer.beikang.act.product.BKProductListBean;
import cn.wikiflyer.beikang.act.product.ProductInfoAct;
import cn.wikiflyer.beikang.act.product.BKProductListBean.BKProductBean;
import cn.wikiflyer.beikang.bean.base.BKBaseBean;
import cn.wk.libs4a.WKApplication;
import cn.wk.libs4a.net.WKAsyncTaskPro;
import cn.wk.libs4a.utils.WKUmShareUtils;
import cn.wk.libs4a.view.list.WKListViewInterface;
import cn.wk.libs4a.view.list.WKListViewUtils;
import cn.wk.libs4a.view.list.WKViewHolder;
import cn.wk.libs4a.view.pulltorefresh.PullToRefreshListView;

import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;

public class ProductAct extends BaseActivity implements WKListViewInterface {

	private WKListViewUtils<BKProductBean2, BKProductListBean> listUtils;

	@Override
	public void setData() {
		// listUtils.updateData();
	}

	private void initList() {
		PullToRefreshListView pList = (PullToRefreshListView) findViewById(R.id.wk_pull_refresh_list);
		listUtils = new WKListViewUtils<BKProductBean2, BKProductListBean>() {
		};
		listUtils.init(ctx, this, pList);
	}

	@Override
	public void loadXml() {
		setContentView(R.layout.bk_act_product);
	}

	@Override
	public void init() {
		// initList();

		startActivity(new Intent().setClass(this, ProductInfoAct.class));
	}

	@Override
	public String getData(boolean... isLocal) {
		return app().net.getProductList(isLocal);
	}

	@Override
	public void getDataBack(String result, boolean isLocal) {
		Type cvbType = new TypeToken<BKBaseBean<BKProductListBean>>() {
		}.getType();
		Gson gson = new Gson();
		listUtils.bean = gson.fromJson(result, cvbType);
		for (int i = 0; i < (listUtils.bean.data.list.size() + 1) / 2; i++) {
			BKProductBean b1 = listUtils.bean.data.list.get(i * 2);
			BKProductBean b2 = null;
			if (i * 2 + 1 < listUtils.bean.data.list.size()) {
				b2 = listUtils.bean.data.list.get(i * 2 + 1);
			}
			listUtils.list.add(new BKProductBean2(b1, b2));
		}
		// listUtils.list.addAll(listUtils.bean.data.list);
		listUtils.adapter.notifyDataSetChanged();
	}

	@Override
	public WKViewHolder createViewHolder(View arg1) {
		return new MyViewHolder(arg1);
	}

	@Override
	public int getItemViewId() {
		return R.layout.item_product;
	}

	class MyViewHolder extends WKViewHolder<BKProductBean2> {

		ImageView img1;
		TextView tv_name1;
		TextView tv_info1;

		ImageView img2;
		TextView tv_name2;
		TextView tv_info2;

		private BKProductBean2 bean;

		OnClickListener click = new OnClickListener() {
			public void onClick(View v) {
				String id = (String) v.getTag();
				Intent i = new Intent(ctx, ProductInfoAct.class);
				i.putExtra("id", id);
				startActivity(i);
			}
		};

		public MyViewHolder(View view) {
			this.img1 = (ImageView) view.findViewById(R.id.imageView_pp1);
			this.tv_name1 = (TextView) view.findViewById(R.id.textView_name1);
			this.tv_info1 = (TextView) view.findViewById(R.id.textView_info1);
			this.img1.setOnClickListener(click);

			this.img2 = (ImageView) view.findViewById(R.id.imageView_pp2);
			this.tv_name2 = (TextView) view.findViewById(R.id.textView_name2);
			this.tv_info2 = (TextView) view.findViewById(R.id.textView_info2);
			this.img2.setOnClickListener(click);
		}

		@Override
		public void setData(final BKProductBean2 b) {
			this.bean = b;
			tv_name1.setText(b.bean1.title);
			tv_info1.setText(b.bean1.description);
			app().imageLoader.displayImage(b.bean1.thumb, img1);
			img1.setTag(b.bean1.id);
			if (b.bean2 != null) {
				showView2();
				tv_name2.setText(b.bean2.title);
				tv_info2.setText(b.bean2.description);
				app().imageLoader.displayImage(b.bean2.thumb, img2);
				img2.setTag(b.bean2.id);
			} else {
				hideView2();
			}
		}

		private void hideView2() {
			img2.setVisibility(View.INVISIBLE);
			tv_name2.setVisibility(View.INVISIBLE);
			tv_info2.setVisibility(View.INVISIBLE);
		}

		private void showView2() {
			img2.setVisibility(View.VISIBLE);
			tv_name2.setVisibility(View.VISIBLE);
			tv_info2.setVisibility(View.VISIBLE);
		}
	}

	/**
	 * 特殊bean
	 */
	public class BKProductBean2 {
		public BKProductBean bean1;
		public BKProductBean bean2;

		public BKProductBean2(BKProductBean b1, BKProductBean b2) {
			this.bean1 = b1;
			this.bean2 = b2;
		}
	}

}
